package com.ruoyi.project.cms.product.service;

import com.ruoyi.common.constant.ProductConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.product.domain.Product;
import com.ruoyi.project.cms.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @CreateTime 2020/5/19 0:17
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询所有产品信息
     */
    @Override
    public List<Product> findAll(Product product) {
        return productMapper.selectAll(product);
    }

    /**
     * 添加产品时 校验产品名称是否唯一
     */
    @Override
    public String checkPnameUnique(Product product) {
        Long pid = StringUtils.isNull(product.getPid()) ? -1L : product.getPid();
        Product info = productMapper.checkPnameUnique(product.getPname());
        if (StringUtils.isNotNull(info) && info.getPid().longValue() != pid.longValue())
        {
            return ProductConstants.PRODUCT_NAME_NOT_UNIQUE;
        }
        return ProductConstants.PRODUCT_NAME_UNIQUE;
    }

    /**
     * 添加产品
     */
    @Override
    public int addProduct(Product product) {
        return productMapper.insertProduct(product);
    }

    @Override
    public Product findById(Long pid) {
        return productMapper.selectById(pid);
    }

    @Override
    public int changeProduct(Product product) {
        return productMapper.updateProduct(product);
    }

    @Override
    public int removeProductByIds(String ids) {
        Long[] proIds = Convert.toLongArray(ids);
        return productMapper.deleteProductByIds(proIds);
    }

    @Override
    public List<Product> findAllProduct() {
        return productMapper.selectAllProduct();
    }

    @Override
    public int changeProductPic(Product product) {
        return productMapper.updateProductPic(product);
    }


}
